package com.edu.web.controller.info;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.edu.common.exception.ServiceException;
import com.edu.common.utils.file.FileTypeUtils;
import com.edu.common.utils.poi.ExcelReaderUtil;
import java.io.File;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.edu.common.annotation.Log;
import com.edu.common.core.controller.BaseController;
import com.edu.common.core.domain.AjaxResult;
import com.edu.common.enums.BusinessType;
import com.edu.system.domain.TStudyScore;
import com.edu.system.service.ITStudyScoreService;
import com.edu.common.utils.poi.ExcelUtil;
import com.edu.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 成绩Controller
 * 
 * @author xxxx
 * @date 2023-12-29
 */
@RestController
@RequestMapping("/system/score")
public class TStudyScoreController extends BaseController
{
    @Autowired
    private ITStudyScoreService tStudyScoreService;

    /**
     * 查询成绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:score:list')")
    @GetMapping("/list")
    public TableDataInfo list(TStudyScore tStudyScore)
    {
        startPage();
        tStudyScore.setLessonName("总分");
        List<TStudyScore> list = tStudyScoreService.selectScoreList(tStudyScore);
        return getDataTable(list);
    }

    /**
     * 导出成绩列表
     */
//    @PreAuthorize("@ss.hasPermi('system:score:export')")
//    @Log(title = "成绩", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, TStudyScore tStudyScore)
//    {
//        List<TStudyScore> list = tStudyScoreService.selectTStudyScoreList(tStudyScore);
//        ExcelUtil<TStudyScore> util = new ExcelUtil<TStudyScore>(TStudyScore.class);
//        util.exportExcel(response, list, "成绩数据");
//    }

    @Log(title = "成绩", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:score:import')")
    @PostMapping("/importData/{examId}")
    public AjaxResult importData(MultipartFile file, @PathVariable("examId") Long examId) throws Exception {

        if (examId == null || examId == 0){
            throw new ServiceException("请选择考试");
        }

        File file2 = new File("/tmp/"+ RandomUtil.randomString(5) + "." + FileTypeUtils.getFileType(file.getOriginalFilename()));
        FileUtil.writeFromStream(file.getInputStream(), file2);
        List<List<String>> lists = ExcelReaderUtil.readExcel(file2);

        String operName = getUsername();
        String message = tStudyScoreService.importScore(lists, examId, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TStudyScore> util = new ExcelUtil<TStudyScore>(TStudyScore.class);
        util.importTemplateExcel(response, "成绩数据");
    }


    /**
     * 获取成绩详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:score:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tStudyScoreService.selectTStudyScoreById(id));
    }

    /**
     * 新增成绩
     */
    @PreAuthorize("@ss.hasPermi('system:score:add')")
    @Log(title = "成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TStudyScore tStudyScore)
    {
        return toAjax(tStudyScoreService.insertTStudyScore(Collections.singletonList(tStudyScore)));
    }

    /**
     * 修改成绩
     */
    @PreAuthorize("@ss.hasPermi('system:score:edit')")
    @Log(title = "成绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TStudyScore tStudyScore)
    {
        return toAjax(tStudyScoreService.updateTStudyScore(tStudyScore));
    }

    /**
     * 删除成绩
     */
    @PreAuthorize("@ss.hasPermi('system:score:remove')")
    @Log(title = "成绩", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tStudyScoreService.deleteTStudyScoreByIds(ids));
    }
}
